package cn.xspace.library.view;


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xspace.android.xafcommon.been.MediaBean;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.ResourceUtils;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.io.File;

import cn.xspace.library.R;
import cn.xspace.library.adapter.GalleryAdapter;
import cn.xspace.library.adapter.SubViewHolder;
import cn.xspace.library.util.DensityUtil;
import cn.xspace.library.util.DrawableUtils;


/**
 * Wubj 创建于 2017/3/1 0001.
 */
public class GalleryViewHolder extends SubViewHolder<MediaBean> {
    RoundAngleImageView mImg;
    RelativeLayout voiceLayout;
    ImageView recordImg;
    ImageView mDel;
    TextView timeView;
    private Context mContext;
    private int mType = 0;
    GalleryAdapter.IDeleteOne iDeleteListener;

    public GalleryViewHolder(View convertView, Context context, GalleryAdapter.IDeleteOne listener, int type) {
        super(convertView);
        mContext = context;
        mType = type;
        iDeleteListener = listener;
        voiceLayout = (RelativeLayout) convertView.findViewById(R.id.relative_time);
        mImg = (RoundAngleImageView) convertView.findViewById(R.id.imageview_file);
        timeView = (TextView) convertView.findViewById(R.id.textview_time);
        recordImg = (ImageView) convertView.findViewById(R.id.imageview_record);
        mDel = (ImageView) convertView.findViewById(R.id.imageview_delete);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void bindHolder(final MediaBean mediaBean) {
        if (mediaBean != null) {
            if ("1".equals(mediaBean.file_type)) {
                if (mType == 1) {
                    ViewGroup.LayoutParams para = recordImg.getLayoutParams();
                    para.width = DensityUtil.dip2px(mContext, 53);
                    para.height = DensityUtil.dip2px(mContext, 53);
                    recordImg.setLayoutParams(para);
                }
                mImg.setVisibility(View.GONE);
                timeView.setVisibility(View.VISIBLE);
                recordImg.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(mediaBean.file_dutime)) {
                    timeView.setText(String.format(ResourceUtils.getString(R.string.voice_time), mediaBean
                            .file_dutime));
                }
                LogUtil.d("秒数 file_dutime>>  " + mediaBean.file_dutime);

                //语音
                recordImg.setImageResource(R.drawable.icon_record_logo);
                voiceLayout.setBackground(DrawableUtils.getDrawable(mContext, mContext.getResources().getColor(R
                                .color.white), mContext.getResources().getColor(R.color.line_picture),
                        5.0f, GradientDrawable.RECTANGLE));
                recordImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (iDeleteListener != null) {
                            iDeleteListener.onVoiceClick(recordImg, timeView, mediaBean);
                        }
                    }
                });
            } else if ("0".equals(mediaBean.file_type)) {
                int width = 65;
                if (mType == 1) {
                    width = 53;
                }
                ViewGroup.LayoutParams para = mImg.getLayoutParams();
                para.width = DensityUtil.dip2px(mContext, width);
                para.height = DensityUtil.dip2px(mContext, width);
                mImg.setLayoutParams(para);
                mImg.setVisibility(View.VISIBLE);
                mImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (iDeleteListener != null)
                            iDeleteListener.onItemClick(v, mediaBean);
                    }
                });
                timeView.setVisibility(View.GONE);
                recordImg.setVisibility(View.GONE);


//                Picasso.with(mContext)
//                        .load(file1)
//                        .placeholder(R.drawable.chat_default_image)
////                        .centerCrop()
//                        .error(R.drawable.chat_default_image_error)
//                        .into(mImg);

                if (TextUtils.isEmpty(mediaBean.content)) {
//                    Glide.with(mContext)
//                            .lo
                    Glide.with(mContext)
                            .load(mediaBean.file_url)
                            .centerCrop()
                            .placeholder(R.drawable.chat_default_image)
                            .error(R.drawable.chat_default_image_error)
                            .into(mImg);
                    //加载网络图片
//                    Picasso.with(mContext)
//                            .load(mediaBean.file_url)
//
//                            .placeholder(R.drawable.chat_default_image)
//                            .resize(DensityUtil.dip2px(mContext, width), DensityUtil.dip2px(mContext, width))
//                            .centerCrop()
//                            .error(R.drawable.chat_default_image_error)
//                            .into(mImg, new Callback() {
//                                @Override
//                                public void onSuccess() {
//                                    L.v("onSuccess");
////                            mOption.setVisibility(View.VISIBLE);
//                                }
//
//                                @Override
//                                public void onError() {
//                                    L.v("onSuccess");
//                                }
//                            });
                } else {
                    try {
                        //图片
//                        mImg.setBackground(Drawable.createFromPath(mediaBean.content));
                        Glide.with(mContext)
                                .load(new File(mediaBean.content))
                                .centerCrop()
                                .placeholder(R.drawable.chat_default_image)
                                .error(R.drawable.chat_default_image_error)
                                .into(mImg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else if ("5".equals(mediaBean.file_type)) {
                //附件
                if (mType == 1) {
                    ViewGroup.LayoutParams para = recordImg.getLayoutParams();
                    para.width = DensityUtil.dip2px(mContext, 53);
                    para.height = DensityUtil.dip2px(mContext, 53);
                    recordImg.setLayoutParams(para);
                }
                mImg.setVisibility(View.GONE);
                timeView.setVisibility(View.VISIBLE);
                recordImg.setVisibility(View.VISIBLE);
                timeView.setText(mediaBean.file_name);

                //附件
                recordImg.setImageResource(R.drawable.icon_file_attach);
                voiceLayout.setBackground(DrawableUtils.getDrawable(mContext, mContext.getResources().getColor(R
                                .color.white), mContext.getResources().getColor(R.color.line_picture),
                        5.0f, GradientDrawable.RECTANGLE));
                recordImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (iDeleteListener != null)
                            iDeleteListener.onItemClick(v, mediaBean);
                    }
                });

            }
            if (mediaBean.isShowDelete) {
                mDel.setVisibility(View.VISIBLE);
                mDel.setOnClickListener(new View.OnClickListener() {
                    //
                    @Override
                    public void onClick(View v) {
                        iDeleteListener.onDeleteOne(mediaBean);
                    }
                });
            } else {
                mDel.setVisibility(View.GONE);
            }


        }

    }
}
